﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProfManager
{
    public partial class UserSelect : Form
    {

        public string selectedProf;

        public UserSelect(IEnumerable<string> files)
        {
            InitializeComponent();
            int count=0;
            foreach (string s in files)
            {
                comboBox1.Items.Add(s.Substring(s.LastIndexOf("\\")+1));
                count++;
            }

            button3.DialogResult = System.Windows.Forms.DialogResult.OK;
            button1.DialogResult = System.Windows.Forms.DialogResult.Cancel;

            while (comboBox1.Items.Count == 0)
                button2_Click(null, null);

            comboBox1.SelectedIndex = 0;
        }

        private void UserSelect_FormClosing(object sender, FormClosingEventArgs e)
        {
            if ( comboBox1.Items.Count == 0 )
                e.Cancel = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string profName = "";
            if (InputBox("Please Input a Profile Name", "New Profile", ref profName) == System.Windows.Forms.DialogResult.OK)
            {
                comboBox1.Items.Add(profName);
            }
        }

        public static DialogResult InputBox(string title, string promptText, ref string value)
        {
            Form form = new Form();
            Label label = new Label();
            TextBox textBox = new TextBox();
            Button buttonOk = new Button();
            Button buttonCancel = new Button();

            form.Text = title;
            label.Text = promptText;
            textBox.Text = value;

            buttonOk.Text = "OK";
            buttonCancel.Text = "Cancel";
            buttonOk.DialogResult = DialogResult.OK;
            buttonCancel.DialogResult = DialogResult.Cancel;

            label.SetBounds(9, 20, 372, 13);
            textBox.SetBounds(12, 36, 372, 20);
            buttonOk.SetBounds(228, 72, 75, 23);
            buttonCancel.SetBounds(309, 72, 75, 23);

            label.AutoSize = true;
            textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
            buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

            form.ClientSize = new Size(396, 107);
            form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
            form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
            form.FormBorderStyle = FormBorderStyle.FixedDialog;
            form.StartPosition = FormStartPosition.CenterScreen;
            form.MinimizeBox = false;
            form.MaximizeBox = false;
            form.AcceptButton = buttonOk;
            form.CancelButton = buttonCancel;

            DialogResult dialogResult = form.ShowDialog();
            value = textBox.Text;
            return dialogResult;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to delete " + comboBox1.Items[comboBox1.SelectedIndex] + "?", "Delete",MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                comboBox1.Items.RemoveAt(comboBox1.SelectedIndex);
            else
                return;

            while (comboBox1.Items.Count == 0)
                button2_Click(null, null);
            comboBox1.SelectedIndex = 0;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            selectedProf = comboBox1.Items[comboBox1.SelectedIndex].ToString();
        }
    }
}
